package com.example.esc_im_client.ui.newview;

import static androidx.core.content.ContentProviderCompat.requireContext;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.pm.PackageManager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import android.content.Intent;
import android.provider.Settings;
import android.Manifest;
import com.example.esc_im_client.R;

/**
 * @ClassName: NewViewActivity
 * @Description:
 * @Author: 墨渍
 * @Date: 2023/8/24 23:20
 **/
public class NewViewActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_POST_NOTIFICATIONS = 1;

    private static final int REQUEST_CODE_NOTIFICATION_SETTINGS = 1;

    private NotificationManager manager;

    private Notification notification;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actiity_demo_new);
        TextView viewById = findViewById(R.id.demoText);
//        Toolbar customToolbar = findViewById(R.id.custom_toolbar);
//        setSupportActionBar(customToolbar);
        viewById.setText("hello is me");
        Button button = findViewById(R.id.sendMsg);
        Button button1 = findViewById(R.id.endMsg);

        manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
                    != PackageManager.PERMISSION_GRANTED) {
                // 请求 POST_NOTIFICATIONS 权限
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.POST_NOTIFICATIONS}, REQUEST_CODE_POST_NOTIFICATIONS);
//                openNotificationSettings();
            } else {
                NotificationChannel channel = new NotificationChannel("leo", "测试通知", NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(channel);
                notification = new NotificationCompat.Builder(this, "leo").setContentTitle("测试标题")
                        .setContentText("测试内容").setSmallIcon(R.drawable.ic_dashboard_black_24dp).build();
                boolean b = manager.areNotificationsEnabled();
                // 启用震动
                channel.enableVibration(true);

// 设置悬浮窗提示
                channel.setAllowBubbles(true);
                System.out.println("权限是否拥有"+b);
            }

        } else {
            // 当前设备的 API 级别低于 26，不调用 NotificationManager.areNotificationsEnabled() 方法
            // 可以使用其他方法来检查通知是否已启用，或者提示用户手动检查通知设置
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manager.notify(1,notification);
            }
        });
    }

//    private void openNotificationSettings() {
//        Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
//        intent.putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().getPackageName());
//        startActivityForResult(intent, REQUEST_CODE_NOTIFICATION_SETTINGS);
//    }
}
